package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.FansListAdapter;
import com.xgkj.diyiketang.activity.data.STMemberAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.ClubMemberListTask;
import com.xgkj.diyiketang.task.FansListTask;
import com.xgkj.diyiketang.task.STTiRenTask;
import com.xgkj.diyiketang.task.TuiChuSheTuanTask;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.MyLoadDataListView;

import java.util.ArrayList;
import java.util.List;

public class STMemberListActivity extends BaseActivity implements MyLoadDataListView.LoadDataListener,View.OnClickListener,STMemberAdapter.OnAdapterClickListener{

    private Context mContext;
    private MyLoadDataListView mListView;
    private STMemberAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int mFansListTaskId = -1;
    private TextView back ;
    private String clubId;
//    private TextView applyText;
    private String creatorId;
    private int managerTaskId = -1;
    private Button tuichuBtn;
    private int tuichuTaskId = -1;
    private String isCLub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stmemberlist);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        clubId = intent.getStringExtra("ID");
        creatorId = intent.getStringExtra("CRID");
        isCLub = intent.getStringExtra("ISCLUB");


        mListView = (MyLoadDataListView) findViewById(R.id.member_list);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mAdapter = new STMemberAdapter(mContext);
        mAdapter.setOnAdapterClickListener(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnLoadDataListener(this);
//        applyText = (TextView) findViewById(R.id.applylist);

        String userId = EatShowApp.getInstance().getmUserInfo().getUserId();
        if(userId.equals(creatorId)){
            mAdapter.setType(1);
        }

        tuichuBtn = (Button) findViewById(R.id.tuichu);
        tuichuBtn.setOnClickListener(this);
        if(!TextUtils.isEmpty(isCLub)){
            if(isCLub.equals("1")){
                tuichuBtn.setVisibility(View.GONE);
            }else {
                tuichuBtn.setVisibility(View.VISIBLE);
            }
        }

        getFansList();
    }

    private void getFansList() {
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        if(mFansListTaskId != -1){
            return ;
        }
        ClubMemberListTask task = new ClubMemberListTask(size,currentPage,clubId);
        task.addListener(mFansListTaskEventListener);
        mFansListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void doReceiveFriend(String userid){
        STTiRenTask task = new STTiRenTask(clubId,userid);
        task.addListener(mFansListTaskEventListener);
        managerTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void doTuiChuST(){
        TuiChuSheTuanTask tcTask = new TuiChuSheTuanTask(clubId);
        tcTask.addListener(mFansListTaskEventListener);
        tuichuTaskId = getEatShowApp().getTaskManager().addTask(tcTask);
    }

    @Override
    public void onLoadMore() {
        getFansList();
    }

    private TaskEventListener mFansListTaskEventListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mFansListTaskId){
                mFansListTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.enableLoadMore();
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    fansList = (List<EatShowUserInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;
            }else if (taskId == managerTaskId){
                currentPage = 1;
                getFansList();
            }else if(taskId ==tuichuTaskId){
                ToastUtil.showMessage(mContext,"退出社团成功!");
                finish();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tuichu:
                DialogUtil.createDialog(mContext, -1, getString(R.string.prompt), getString(R.string.sure_tuichu), getString(R.string.ok), getString(R.string.cancle), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        doTuiChuST();
                    }
                }, null);
                break;
        }
    }

    @Override
    public void onReceiveClick(String userId) {
        doReceiveFriend(userId);
    }
}
